package dao;

import domain.Emp;
import utils.JdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UpEmpDaoImp implements UpEmpDao {
    //通过id得到emp的值
    @Override
    public Emp getEmp(int id) {
        Connection conn=null;
        PreparedStatement sta=null;
        ResultSet rs=null;
        Emp emp =null;
        try {
            conn= JdbcUtils.getConnection();
            //编写预编译对象
            String sql="select *from t_emp where id=?;";
            //获得预编译对象
            sta=conn.prepareStatement(sql);
            //设置值
            sta.setInt(1,id);
            //运行得到结果集
            rs=sta.executeQuery();
            if(rs.next()){
                emp=new Emp();
                emp.setId(rs.getInt("id"));
                emp.setEname(rs.getString("ename"));
                emp.setAge(rs.getInt("age"));
                emp.setSex(rs.getString("sex"));
                emp.setSal(rs.getDouble("sal"));
                emp.setBirthday(rs.getString("birthday"));
                emp.setEdate(rs.getString("edate"));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JdbcUtils.close(conn,sta,rs);
        }
        return emp;
    }
}
